
#include "Player.h"
#include "World.h"
#include "Team.h"
#include "GameState.h"


Player::Player(unsigned player_id, std::string player_name, unsigned team_id) :
    player_id(player_id),
    name(player_name),
    score(0),
    team_id(team_id),
    total_shots_fired(0),
    total_shots_hit(0),
    last_gaze_point(0.0f, 0.0f) {

}

Player::~Player(){
}

void Player::incrementScore(int amount){ 
    score += amount; 
}

unsigned Player::getScore(void) const { 
    return score;
} 

std::string Player::getName(void) const { 
    return name;
}

Ship* Player::getShip(void) const { 
    return (Ship*)(my_ship_ref.getPointer()); 
}

unsigned Player::getTeamId(void) const { 
    return team_id; 
}

int Player::getShotsFired(void) const { 
    return total_shots_fired; 
}


int Player::getShotsHit(void) const { 
    return total_shots_hit; 
}

Vector2D Player::getFocusPoint(void) const {
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship != NULL){
        last_gaze_point = pship->getPosition();
    }
    return last_gaze_point;
}

void Player::incShotsFired(void){ 
    total_shots_fired++; 
}

void Player::incShotsHit(void){ 
    total_shots_hit++; 
}    

bool Player::amAlive(void) const {
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(my_ship_ref.isValid() && pship->getHealth() > 0){ return true; }
    else{ return false; }
}

void Player::checkHealth(void){
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship != NULL && pship->getHealth() <= 0){ pship->die(); }
}

void Player::spawnNewShip(void){
    Team &my_team(GameState::instance().getTeam(team_id));
    Ship *new_ship = new Ship(this, my_team.getRandomSpawnPoint());
    World::instance().telefrag(new_ship->getPosition(), new_ship->getSize());
    my_ship_ref = World::instance().addObject(new_ship);
}
    

